<?php
/**
 * general.functions.php
 * 
 * directory:	includes/
 * author:		wil hamilton <wil.hamilton@gmail.com>
 * written:		april 1, 2008
 * updated:		
 *
 * purpose:		
 *
 * defines functions that are required for operation
 *
 */
 

#define the autoload function to load classes dynamically as they're called
function __autoload ($className)
{
	// we take the 'name' of the class and translate it into an absolute filepath
	
	
	if(strpos($className, '_'))	//the class contains and underscore, its definately a module class
	{
		$class = explode('_', $className);
		if($class[1] == 'module')
		{
			$className = MODULE_DIR . $class[0] . '/'. $class[1] .'.php';
		}
		else
		{
			$className = MODULE_DIR . $class[0] . '/classes/'. $class[1] .'.class.php';
		}
		include($className);
	}
	else	//the class could be in the core, or it could be a standalone module class
	{
		$class = ROOT . 'core/classes/' . $className . '.class.php';
		
		if(file_exists($class)) //couldnt include this class, check stand alone classes
		{
			
			include($class);
		}
		else
		{
			$class = MODULE_DIR . $className . '/classes/'. $className .'.class.php';
			include($class);
		}
	}
	
}

#define a function to format exceptions
function exceptionHandler($e)
{
	echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
		  <html>
		  <header>
			<title>Fatal Error!</title>
		  </header>
		  <body>
			<h1>Fatal Error!</h1>
			<p>A fatal error has occured.  The following error message was produced: ' . $e->getMessage() . '</p>
			<p>The error occured in file: ' . $e->getFile() . '</p>
			<p>The error occured on line: ' . $e->getLine() . '</p>
			<p>The traceback is as follows: ' . $e->getTraceAsString() . '</p>
		  </body>
		  </html>';
}

#set this function as the exception handler
set_exception_handler('exceptionHandler');

?>